package com.lss.cloud.mygateway;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.filter.OrderedFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 2024/3/18-22:43
 * 功能:
 */
@Component
@Slf4j
public class MyGlobalFilter implements GlobalFilter, Ordered {
    private static final String begin_time = "beginTime";
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        exchange.getAttributes().put(begin_time, System.currentTimeMillis());

        return chain.filter(exchange).then(Mono.fromRunnable(()->
                {   Long beginTime = exchange.getAttribute(begin_time);
                    if (exchange.getAttribute(begin_time) != null){
                        log.info("唯一标识：{}",exchange.getRequest().getURI());
                        log.info("持续时间"+(System.currentTimeMillis()-beginTime));
                        System.out.println("=================================================");
                    }
                }
                ));
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
